package cn.stylefeng.guns.modular.business.frequency.orderinsurance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderinsurance.entity.OrderInsurance;
import cn.stylefeng.guns.modular.business.frequency.orderinsurance.mapper.OrderInsuranceMapper;
import cn.stylefeng.guns.modular.business.frequency.orderinsurance.model.param.OrderInsuranceParam;
import cn.stylefeng.guns.modular.business.frequency.orderinsurance.service.OrderInsuranceService;
import cn.stylefeng.guns.modular.business.frequency.orderinsurance.enums.OrderInsuranceExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单生成保单合同表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderInsuranceServiceImpl extends ServiceImpl<OrderInsuranceMapper, OrderInsurance> implements OrderInsuranceService {

    @Override
    public PageResult<OrderInsurance> page(OrderInsuranceParam orderInsuranceParam) {

        // 构造条件
        LambdaQueryWrapper<OrderInsurance> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderInsurance> list(OrderInsuranceParam orderInsuranceParam) {

        // 构造条件
        LambdaQueryWrapper<OrderInsurance> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderInsuranceParam orderInsuranceParam) {

        // 将dto转为实体
        OrderInsurance orderInsurance = new OrderInsurance();
        BeanUtil.copyProperties(orderInsuranceParam, orderInsurance);

        this.save(orderInsurance);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderInsuranceParam orderInsuranceParam) {
        this.removeById(orderInsuranceParam.getId());
    }

    @Override
    public void edit(OrderInsuranceParam orderInsuranceParam) {

        // 根据id查询实体
        OrderInsurance orderInsurance = this.queryOrderInsurance(orderInsuranceParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderInsuranceParam, orderInsurance);

        this.updateById(orderInsurance);
    }

    @Override
    public OrderInsurance detail(OrderInsuranceParam orderInsuranceParam) {
        return this.queryOrderInsurance(orderInsuranceParam);
    }



    /**
     * 获取订单生成保单合同表
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderInsurance queryOrderInsurance(OrderInsuranceParam orderInsuranceParam) {
        OrderInsurance orderInsurance = this.getById(orderInsuranceParam.getId());
        if (ObjectUtil.isEmpty(orderInsurance)) {
            throw new ServiceException(OrderInsuranceExceptionEnum.NOT_EXISTED);
        }
        return orderInsurance;
    }
}